package com.tomcov.client.activities;


import java.util.Date;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.tomcov.client.ClientFactory;
import com.tomcov.client.i18n.MessageViewConstants;
import com.tomcov.client.place.WelcomPlace;
import com.tomcov.client.service.MessageServiceAsync;
import com.tomcov.client.service.UserServiceAsync;
import com.tomcov.client.view.CreatUserView;
import com.tomcov.shared.Message;
import com.tomcov.shared.UserInfo;

public class CreatUserActivity extends AbstractActivity implements CreatUserView.Presenter{
	private MessageViewConstants ressource= GWT.create(MessageViewConstants.class);
	private ClientFactory clientFactory;

	/**
	 * Instantiates a new creat user activity.
	 *
	 * @param clientFactory the client factory
	 */
	public CreatUserActivity(ClientFactory clientFactory){
		this.clientFactory = clientFactory;	
	}

	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		CreatUserView creatUserView = clientFactory.getCreatUserView();
		creatUserView.getNextButton().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				CreatUserView creatUserView = clientFactory.getCreatUserView();
				creatUserView.getNextButton().setEnabled(false);
				creatUser();
				creatUserView.getNextButton().setEnabled(true);
			}
		});
		creatUserView.setPresenter(this);
		panel.setWidget(creatUserView.asWidget());
	}

	@Override
	public void goTo(Place place) {
		clientFactory.getPlaceController().goTo(place);			
	}

	public void creatUser(){
		final CreatUserView creatuserview= clientFactory.getCreatUserView();
		UserServiceAsync USA = clientFactory.getUSA();

		if(creatuserview.getUserName().getValue()== null || creatuserview.getFirstName().getValue()==null||
				creatuserview.getLastName().getValue()==null ||creatuserview.getAdressEmail().getValue()==null||
				creatuserview.getPassword().getValue()==null){
			Window.alert("fill all the field");
		}

		else  if(creatuserview.getAdressEmail().getValue().matches("^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$")==false){
			Window.alert("adresse email invalide");
		}

		else{	
			UserInfo user= new UserInfo(creatuserview.getUserName().getValue(),creatuserview.getAdressEmail().getValue(),
					creatuserview.getPassword().getValue(),creatuserview.getFirstName().getValue(),
					creatuserview.getLastName().getValue(),creatuserview.getLangage());
	
			USA.addUser(user,new AsyncCallback<UserInfo>(){
				@Override
				public void onFailure(Throwable caught) {
					try{
						throw caught;
					}
					catch (Throwable e) {
						Window.alert("server failed");
						e.printStackTrace();
					}

				}
				@Override
				public void onSuccess(UserInfo result) {
					if (result==null){
						Window.alert("this name is already used");
					}
					else{
						Window.alert("user created");
						goTo(new WelcomPlace(null));
						MessageServiceAsync MSA=clientFactory.getMSA();
						Date today= new Date();
						Message newmessage = new Message(result.getUserName(),result.getUserName(),ressource.welcommessage(),today.toString() );
						MSA.AddMessage(newmessage, new AsyncCallback<Message>(){
							@Override
							public void onFailure(Throwable caught) {
								Window.alert("fail to sent welcom message");
							}
							@Override
							public void onSuccess(Message result) {	
							}
						});		
					}

				}
			});
		}

	}
}